#include "file.h"
#include "ui_file.h"
#include <QDebug>
#include <QFileDialog>
#include <QColorDialog>

file::file(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::file)
{
    ui->setupUi(this);
}

file::~file()
{
    delete ui;
}

void file::on_file_bt_clicked()
{
    QString file_path = QFileDialog::getOpenFileName(this,"选择文件","./","*");
    qDebug()<<file_path;
    //直接是用fileinfo的构造函数构造对象
    QFileInfo file(file_path);
    qDebug()<<file.path();
    //使用QFileInfo的对象去实现名字与路径的拆分
    //    QString filename = file.fileName();//只有文件名
    //    QString filepath =file.filePath(); //文件路径+文件名
    //    QString path  = file.path();//只有文件路径
    //    ui->file_list->addItem(filename);
    //    ui->file_list->addItem(filepath);
    //    ui->file_list->addItem(path);
    ui->file_list->addItem(file_path);
}

void file::on_open_file_clicked()
{
//    if(ui->file_list->currentItem())
//    {

//    }

    //拿到文件名   //一定要先选中
    QString text =  ui->file_list->currentItem()->text();
    //设置文件路径+文件名
    fil.setFileName(text);
    qDebug()<<text;
    //打开文件
    fil.open(QIODevice::ReadOnly);
    //读取里面的文件
    QByteArray arr = fil.readAll();
    //设置在文本框中
    ui->text->setText(arr);
}

void file::on_font_color_clicked()
{
    QColor  mycolor = QColorDialog::getColor(Qt::white,this,"拿颜色");
    font_color =QString("color:%1;").arg(mycolor.name());
    ui->text->setStyleSheet(font_color+background_color);
}

void file::on_font_bcolor_clicked()
{
    QColor  mycolor = QColorDialog::getColor(Qt::white,this,"拿颜色");
    background_color =QString("background-color:%1;").arg(mycolor.name());
    ui->text->setStyleSheet(font_color+background_color);
}

void file::on_save_clicked()
{
    QString text = ui->text->toPlainText();
    QString path = fil.fileName();
    QFile file;
    file.setFileName(path);
    file.open(QIODevice::WriteOnly);
    file.write(text.toUtf8());
    file.close();
}


void file::on_back_clicked()
{
    this->parentWidget()->show();
    this->hide();
}
